package com.ymttest.database.sqlwapper;

import java.util.Calendar;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtProductsSubsidy;
import com.ymttest.utils.EnvSetup;

/************************************************************************************
 * @File name : YmtUserWapper.java
 * @Author : zhouyi
 * @Date : 2015年4月2日
 * @Copyright : 洋码头
 ************************************************************************************/
public class YmtProductsSubsidyWapper {
	SqlManager manage;

	public YmtProductsSubsidyWapper() {
		manage = new SqlManager("ymt" + EnvSetup.getEnv());
	}

	/**
	 * insert 一条YmtProductsSubsidy记录
	 * 
	 * @param iactivityid
	 *            活动id
	 * @param sproductid
	 *            商品id
	 * @param iproductinactivityid
	 *            商品活动id
	 * @param iuserid
	 *            活动发起商户id
	 * @return
	 * @throws InterruptedException
	 */
	public int insertOneProductsSubsidy(int iactivityid, String sproductid, int iproductinactivityid, int iuserid)
			throws InterruptedException {
		Calendar calendar = Calendar.getInstance();
		YmtProductsSubsidy record = new YmtProductsSubsidy();
		record.setIactivityid(iactivityid);
		record.setSproductid(sproductid);
		record.setIsubsidytype(1);
		record.setFsubsidyprice("9.98");
		record.setIproductinactivityid(iproductinactivityid);
		record.setDaddtime(calendar.getTime());

		int i = manage.insert("com.ymttest.database.mapping.YmtProductsSubsidyMapper.insert", record);
		manage.commit();
		// manage.close();
		return i;
	}

	/**
	 * insert 一条YmtProductsSubsidy记录
	 * 
	 * @param record
	 *            YmtProductsSubsidy
	 * @return
	 * @throws InterruptedException
	 */
	public int insertProductsSubsidy(YmtProductsSubsidy record) throws InterruptedException {
		int i = manage.insert("com.ymttest.database.mapping.YmtProductsSubsidyMapper.insert", record);
		manage.commit();
		// manage.close();
		return i;
	}

	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}

	public static void main(String args[]) {
		try {
			int isellerid = 3383;
			// 新建一个活动
			new YmtActivityWapper().insertOneActivity(isellerid);
			// 获取活动id
			int iactivityid = new YmtActivityWapper().getLastOneRecord(isellerid).getIactivityid();
			// 获取 sproductid
			String sproductid = new YmtProductsWapper().selectByiUserId(String.valueOf(isellerid)).getSproductid();
			// 新建一个商品报名表记录
			new YmtProductsInActivityWapper().insertOneProductsInActivity(iactivityid, sproductid, 1, isellerid);
			// 获取商品活动id
			Integer paid = new YmtProductsInActivityWapper().selectByiActivityId(iactivityid).getIproductinactivityid();
			// 新建一个新人价商品
			new YmtProductsSubsidyWapper().insertOneProductsSubsidy(iactivityid, sproductid, paid, 3383);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
